package com.lmonkey.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebFilter("/shangc/domimacheck")
public class DoMimaCheck implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest reqq = (HttpServletRequest)req;
        HttpServletResponse respp = (HttpServletResponse) resp;
        //设置字符集
        reqq.setCharacterEncoding("UTF-8");
        respp.setContentType("text/html;charset=utf-8");

        String flag = reqq.getParameter("flag");
        PrintWriter out = respp.getWriter();
        //验证原密码
        String pass = reqq.getParameter("pass");
        if (pass.equals("")){
            if (flag != null){
                out.println("<script>alert('请原密码');location.href='http://localhost:8080/lyq/admin/admin_xuigai.jsp';</script>");
            }else {
                out.println("<script>alert('请原密码');location.href='/lyq/shangc/remimaselect';</script>");
            }
            out.close();
            return;
        }

        //验证新密码和确认密码
        String newpass = reqq.getParameter("newpass");
        String againpass = reqq.getParameter("againpass");
        if (newpass.equals("")|| againpass.equals("")){
            if (flag != null){
                out.println("<script>alert('新密码或确认密码不能为空');location.href='http://localhost:8080/lyq/admin/admin_xuigai.jsp';</script>");
            }else {
                out.println("<script>alert('新密码或确认密码不能为空');location.href='/lyq/shangc/remimaselect';</script>");
            }

            out.close();
            return;
        }else if (!newpass.equals(againpass)){
            if (flag != null){
                out.println("<script>alert('两次密码输入不一致');location.href='http://localhost:8080/lyq/admin/admin_xuigai.jsp';</script>");
            }else {
                out.println("<script>alert('两次密码输入不一致');location.href='/lyq/shangc/remimaselect';</script>");
            }

            out.close();
            return;
        }else{
            Pattern pt = Pattern.compile("^[A-Za-z0-9-]{6,15}$");
            Matcher mt = pt.matcher(newpass);
            if (!mt.matches()){
                if (flag != null){
                    out.println("<script>alert('新密码格式有误');location.href='http://localhost:8080/lyq/admin/admin_xuigai.jsp';</script>");
                }else {
                    out.println("<script>alert('新密码格式有误');location.href='/lyq/shangc/remimaselect';</script>");
                }

                out.close();
                return;
            }
        }


        //验证验证码是否正确
        if (flag == null){
            HttpSession session = reqq.getSession();
            String verycode = reqq.getParameter("verycode");
            String syscode = (String)session.getAttribute("code");
            if (verycode.equals("")){
                out.println("<script>alert('验证码不能为空');location.href='/lyq/shangc/remimaselect';</script>");
                out.close();
                return;
            }else  if (!syscode.equals(verycode)){
                out.println("<script>alert('验证码输入有误');location.href='/lyq/shangc/remimaselect';</script>");
                out.close();
                return;
            }
        }

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
